@include("user.head")
	<!-- 公共头部 -->

	<div id="content_person">
		<div class="row">
			<div class="col-xs-3">
				@include("user.uc.navigation")
			</div>
			<div class="col-xs-9">
				<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">添加/编辑技术</h3>
				</div>
				<div class="panel-body">
						<div class="form-horizontal" action="edit_tech/add">
						  <fieldset>
							<div class="form-group row">
							<div class="form-group">
							  <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术名称:</label>
							  <div class="col-xs-6">
							    <input class="form-control input_size" id="inputEmail" placeholder="" name="tech_name" type="text">
							  </div>
							</div>

						    <div class="form-group">
						      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术开发者:</label>
						      <div class="col-xs-6">
						        <input class="form-control input_size" id="inputEmail" placeholder="" name="developer" type="text">
						      </div>
						    </div>


						    <div class="form-group">
						      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术优势介绍:</label>
						      <div class="col-xs-6">
						        <textarea class="form-control text_size" rows="3" name="advantage_des" id="textArea"></textarea>
						        <span class="help-block"></span>
						      </div>
						    </div>

						    <div class="form-group">
						      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术原理介绍:</label>
						      <div class="col-xs-6">
						        <textarea class="form-control text_size" rows="3" name="theory_des" id="textArea"></textarea>
						        <span class="help-block"></span>
						      </div>
						    </div>

						    <div class="form-group">
						      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术评价:</label>
						      <div class="col-xs-6">
						        <input class="form-control input_size" id="inputEmail" placeholder="" name="score" type="text">
						      </div>
						    </div>

						    <div class="form-group">
						      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">典型客户:</label>
						      <div class="col-xs-6">
						        <input class="form-control input_size" id="inputEmail" placeholder="" name="typical_client" type="text">
						      </div>
						    </div>

						    <div class="form-group">
						      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术图片:</label>
						      <div class="col-xs-6">
						        <a href="javascript:;" class="file btn btn-primary">上传
						            <input type="file" name="imgFile" id="pic">
						            <input type="hidden" class="hiddenName" name="image">
						        </a><span class="fileName"></span>
						      </div>
						    </div>

						    <div class="form-group">
					      		<label for="select" class="col-xs-3 control-label no_padding_right">应用技术:</label>
						      	<div class="col-xs-6">
						        <select class="form-control input_size" name="tech_id">
						          <option value="">请选择</option>
						          @foreach($techs as $value)
						          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
						          @endforeach
						        </select>
						      </div>
					    	</div>

					    	<div class="form-group" >
					      		<label for="select" class="col-xs-3 control-label no_padding_right">应用领域:</label>
						      	<div class="col-xs-6">
						        <select class="form-control input_size" name="domain_id" id="domain">
						          <option value="0">请选择</option>
						          @foreach($domains as $value)
						          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
						          @endforeach
						        </select>
						     	</div>
					    	</div>

					    	<div class="form-group" >
					      		<label for="select" class="col-xs-3 control-label no_padding_right"></label>
					     	 	<div class="col-xs-6" id="domains">
					     		</div>
					    	</div>

		        		    <div class="form-group mainBody">
		        		      <label class="col-xs-3 control-label no_padding_right">应用主体:</label>
		        		      <div class="col-xs-6">
		        		        <select class="form-control input_size main_body">
				    	          	<option value="">请选择</option>
				    	          	@foreach ($mBody as $value)
				    	          	<option value="<%$value['id']%>"><%$value['name']%></option>
				    	          	@endforeach
		        		        </select>
		        		      </div>
		        		      <div class="col-xs-3 no_padding_both">
		        		      	<button class="btn btn-primary" id="addMain">添加</button>
		        		      </div>
		        		    </div>

		        		    <div class="form-group">
		        		      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">节能效率:</label>
		        		      <div class="col-xs-6">
		        		        <input class="form-control input_size" name="efficiency" autocomplete="of" id="inputEmail" placeholder="" type="text">
		        		      </div>
		        		    </div>

		        		    <div class="form-group">
		        		      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术描述:</label>
		        		      <div class="col-xs-6">
		        		        <textarea class="form-control text_size" rows="3" name="description" id="textArea"></textarea>
		        		        <span class="help-block"></span>
		        		      </div>
		        		    </div>

			    	    	<div class="form-group">
			    	      		<label for="select" class="col-xs-3 control-label no_padding_right">技术原理概述:</label>
			    	      		<div class="col-xs-9">
									<div id="container" name="theory" style="height:500px;"></div>
				    	    	</div>
			    	    	</div>
					    	<div class="form-group">
						      <div class="col-xs-12 text-center">
						      	<input type="hidden" name="tid">
						        <button type="button" id="sub" class="btn btn-primary ">提交</button>
						      </div>
						    </div>
						  </fieldset>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- 公共底部 -->
	@include("user.footer")
	@include("user.common")

	<!-- 公共底部 -->
<script type="text/html" id="mainbodyTemp">
<option value="">请选择</option>
{%each list as value}
	<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>

<script type="text/html" id="mainSelectTemp">
<div class="form-group mainBody">
  <label class="col-xs-3 control-label no_padding_right"></label>
  <div class="col-xs-6">
    <select class="form-control input_size main_body">
    </select>
  </div>
  <div class="col-xs-3 no_padding_both">
  	<button class="btn btn-warning deleteMain">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="domain_type">
	<select class="form-control input_size" name="domain_ids">
		{%each list as value index}
		<option value="{%value.id}">{%value.cate_name}</option>
		{%/each}
	</select>
</script>
<script type="text/javascript">
var ue = UE.getEditor('container');

$("#addMain").bind("click", function(){
	var html = template("mainSelectTemp", {});
	$(html).insertAfter($(".mainBody").last());
	loadMain();
	$(".deleteMain").unbind("click");
	$(".deleteMain").bind("click", function(){
		$(this).parents(".mainBody").remove();
	})
})

function loadMain(){
	$.ajax({
		url : "<%url('uc/get_mainbody')%>",
		type : "get",
		async : isasync,
		success : function(data){
			if(data.success && data.data.length)
			{
				console.log(data.success)
				var str = template("mainbodyTemp", {list : data.data});
				$(".main_body").last().html(str);
			}
		}
	})
}

$('#pic').AjaxFileUpload({
	path : "<% url('uc/user/upload') %>",
	onComplete : function(filename){
			$(this).siblings('input[class="hiddenName"]').val(filename.filename);
			$(this).parent(".file").siblings(".fileName").html(filename.filename);
		}
	});

var isasync = true;
var url = "edit_tech/add";
$("#domain").change(function(){
	var parent_id = $(this).val();
	$.ajax({
		url : "<%url('uc/get_domain_type')%>",
		type : "get",
		data : {parent_id : parent_id},
		async: isasync,
		dataType : "json",
		success:function(data){
			if(data.success){
				var html = template("domain_type", {list: data.data});
				$("#domains").html(html);
			}
		},
		error:function(data){
			var html = '';
			$("#domains").html(html);
		}
	})

})


if("<%Input::has('tid')%>"){
	var tid = "<%Input::get('tid')%>";
	$.get('edit_tech/get_tech', {tid : tid}, function(data){
		if(data.success){
			isasync = false;
			$('input[name=tech_name]').val(data.tech_name);
			$('input[name=developer]').val(data.developer);
			$("textarea[name='advantage_des']").val(data.advantage_des);
			$("textarea[name='theory_des']").val(data.theory_des);
			$("textarea[name='description']").val(data.description);
			$("input[name='typical_client']").val(data.typical_client);
			$("input[name='score']").val(data.score);
			$("input[name='efficiency']").val(data.efficiency);
			$("input[name='image']").val(data.image);
			$("select[name='tech_id']").val(data.tech_tech.cate_id);
			$("select[name='domain_id']").val(data.tech_domain.domain.parent_id);
			$("select[name='domain_id']").change();
			$("select[name='domain_ids']").val(data.tech_domain.domain.id);
			$("input[name='tid']").val(data.id);

			$.each(data.mainbody, function(k, v){
				$(".main_body").eq(k).val(v.mb_id);
				$("#addMain").click();
			})
			$(".deleteMain").last().click();

			isasync = true;
			ue.ready(function(){
			    ue.setContent(decodeURIComponent(data.theory));
			});
			url = "edit_tech/update";
		}else{
			window.location.href="edit_tech";
		}
	})
}

$("#sub").click(function(){

	var domain = $("select[name='domain_id']").val();
	var domains = $("select[name='domain_ids']").val();
	if(domains)
	{
		var domain_id = domains;
	}
	else
	{
		var domain_id = domain;
	}
	var tech_id = $("select[name='tech_id']").val();
	var tech_name = $("input[name='tech_name']").val().trim();
	var developer = $("input[name='developer']").val().trim();
	var efficiency = $("input[name='efficiency']").val().trim();
	var theory = encodeURIComponent(ue.getContent());
	var advantage_des = $("textarea[name='advantage_des']").val().trim();
	var score = $("input[name='score']").val().trim();
	var description = $("textarea[name='description']").val().trim();
	var typical_client = $("input[name='typical_client']").val().trim();
	var theory_des = $("textarea[name='theory_des']").val().trim();
	var image = $("input[name='image']").val().trim();
	var tid = $("input[name='tid']").val().trim();

	var main_body = [];
	$(".main_body").each(function(k, v){
		if($(v).val() > 0){
			main_body.push($(v).val());
		}
	})


	if (developer == '' || tech_name == '' || advantage_des == '' || score == '' || image == '' || theory == '' || main_body == '' || description == '')
	{
		warning('','必填信息不能为空');
		return false;
	}
	$.post(url, {
	tech_name : tech_name,
	developer : developer,
	theory : theory,
	description : description,
	advantage_des : advantage_des,
	theory_des : theory_des,
	score : score,
	image : image,
	efficiency : efficiency,
	typical_client : typical_client,
	domain_id : domain_id,
	tech_id : tech_id,
	tid : tid,
	main_body : main_body
	},function (data){
		if(data.success){
			success("", "提交成功");
			setTimeout(function(){
				window.location.href="tech";
			},2000)
		}
		else
		{
			warning("", data.msg);
		}
	})

})
</script>
</body>
</html>